package net.dreamlu.qa.validator;

import net.dreamlu.qa.model.User;
import net.dreamlu.util.MD5Util;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

public class SettingValidator extends Validator {
	
	private User user;
	protected void validate(Controller controller) {
		setShortCircuit(true);
		user = controller.getSessionAttr("user");
		
		validateString("oldpassword", 6, 20, "errorPwdOld", "1");
		validateEqualString(MD5Util.getMD5Str(controller.getPara("oldpassword")), user.getStr("password"), "errorPwdOld", "1");
		validateString("password", 6, 20, "errorPwd", "1");
		validateEqualField("password", "confirmpassword", "errorPwdConfirm", "1");
	}
	
	protected void handleError(Controller controller) {
		controller.keepModel(User.class);
		controller.setAttr("tab", controller.getPara("tab"));
		controller.render("/account/settings.jsp");
	}
}